package com.dzt.cloudreader.adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.dzt.cloudreader.R;
import com.dzt.cloudreader.adapter.recycler.RecyclerAdapter;
import com.dzt.cloudreader.adapter.recycler.RecyclerHolder;
import com.dzt.cloudreader.interfaces.PerfectClickListener;
import com.dzt.cloudreader.model.GankIoDayBean.ResultsBean.AndroidBean;
import com.dzt.cloudreader.rxbus.RxBus;
import com.dzt.cloudreader.rxbus.RxCodeConstants;
import com.dzt.cloudreader.utils.ImgLoadUtil;
import com.dzt.cloudreader.webview.WebViewActivity;

import java.util.List;

/**
 * Created by M02323 on 2017/12/23.
 */

public class EverydayAdapter extends RecyclerAdapter<List<AndroidBean>>{
	private static final int TYPE_TITLE = 1; // title
	private static final int TYPE_ONE = 2;// 一张图
	private static final int TYPE_TWO = 3;// 二张图
	private static final int TYPE_THREE = 4;// 三张图

	public EverydayAdapter(Context context, List<List<AndroidBean>> datas, int layoutId) {
		super(context, datas, layoutId);
	}

	@Override
	public int getItemViewType(int position) {
		if (!TextUtils.isEmpty(getData().get(position).get(0).getType_title())) {
			return TYPE_TITLE;
		} else if (getData().get(position).size() == 1) {
			return TYPE_ONE;
		} else if (getData().get(position).size() == 2) {
			return TYPE_TWO;
		} else if (getData().get(position).size() == 3) {
			return TYPE_THREE;
		}
		return super.getItemViewType(position);
	}

	@Override
	public RecyclerHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		switch (viewType) {
			case TYPE_TITLE:
				return new RecyclerHolder(context,
						inflater.inflate(R.layout.item_everyday_title, parent, false));
			case TYPE_ONE:
				return new RecyclerHolder(context,
						inflater.inflate(R.layout.item_everyday_one, parent, false));
			case TYPE_TWO:
				return new RecyclerHolder(context,
						inflater.inflate(R.layout.item_everyday_two, parent, false));
			default:
				return new RecyclerHolder(context,
						inflater.inflate(R.layout.item_everyday_three, parent, false));
		}
	}

	private void setDes(List<AndroidBean> object, int position, TextView textView) {
		textView.setText(object.get(position).getDesc());
	}

	@Override
	public void convert(RecyclerHolder holder, List<AndroidBean> data, int position) {
		if(getItemViewType(position) == TYPE_TITLE){
			int index = 0;
			String title = data.get(0).getType_title();
			holder.setText(R.id.tv_title_type, title);
			if ("Android".equals(title)) {
				holder.setImageResource(R.id.iv_title_type, R.mipmap.home_title_android);
				index = 0;
			} else if ("福利".equals(title)) {
				holder.setImageResource(R.id.iv_title_type, R.mipmap.home_title_meizi);
				index = 1;
			} else if ("IOS".equals(title)) {
				holder.setImageResource(R.id.iv_title_type, R.mipmap.home_title_ios);
				index = 2;
			} else if ("休息视频".equals(title)) {
				holder.setImageResource(R.id.iv_title_type, R.mipmap.home_title_movie);
				index = 2;
			} else if ("拓展资源".equals(title)) {
				holder.setImageResource(R.id.iv_title_type, R.mipmap.home_title_source);
				index = 2;
			} else if ("瞎推荐".equals(title)) {
				holder.setImageResource(R.id.iv_title_type, R.mipmap.home_title_xia);
				index = 2;
			} else if ("前端".equals(title)) {
				holder.setImageResource(R.id.iv_title_type, R.mipmap.home_title_qian);
				index = 2;
			} else if ("App".equals(title)) {
				holder.setImageResource(R.id.iv_title_type, R.mipmap.home_title_app);
				index = 2;
			}

			if (position != 0) {
				holder.findView(R.id.view_line).setVisibility(View.VISIBLE);
			} else {
				holder.findView(R.id.view_line).setVisibility(View.GONE);
			}

			final int finalIndex = index;
			holder.findView(R.id.ll_title_more).setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					RxBus.getDefault().post(RxCodeConstants.JUMP_TYPE, finalIndex);
				}
			});
		}else if(getItemViewType(position) == TYPE_ONE){
			TextView tvOnePhotoTitle = holder.findView(R.id.tv_one_photo_title);
			ImageView ivOnePhoto = holder.findView(R.id.iv_one_photo);
			if ("福利".equals(data.get(0).getType())) {
				tvOnePhotoTitle.setVisibility(View.GONE);
				ivOnePhoto.setScaleType(ImageView.ScaleType.CENTER_CROP);
				holder.setImageGlide(R.id.iv_one_photo, data.get(0).getUrl());
			} else {
				tvOnePhotoTitle.setVisibility(View.VISIBLE);
				setDes(data, 0, tvOnePhotoTitle);
				displayRandomImg(1, 0, ivOnePhoto, data);
			}
			LinearLayout llOnePhoto = holder.findView(R.id.ll_one_photo);
			setOnClick(llOnePhoto, data.get(0));
		}else if(getItemViewType(position) == TYPE_TWO){
			ImageView ivTwoOneOne = holder.findView(R.id.iv_two_one_one);
			ImageView ivTwoOneTwo = holder.findView(R.id.iv_two_one_two);
			TextView tvTwoOneOneTitle = holder.findView(R.id.tv_two_one_one_title);
			TextView tvTwoOneTwoTitle = holder.findView(R.id.tv_two_one_two_title);
			LinearLayout llTwoOneOne = holder.findView(R.id.ll_two_one_one);
			LinearLayout llTwoOneTwo = holder.findView(R.id.ll_two_one_two);
			displayRandomImg(2, 0, ivTwoOneOne, data);
			displayRandomImg(2, 1, ivTwoOneTwo, data);
			setDes(data, 0, tvTwoOneOneTitle);
			setDes(data, 1, tvTwoOneTwoTitle);
			setOnClick(llTwoOneOne, data.get(0));
			setOnClick(llTwoOneTwo, data.get(1));
		}else if(getItemViewType(position) == TYPE_THREE){
			ImageView ivThreeOneOne = holder.findView(R.id.iv_three_one_one);
			ImageView ivThreeOneTwo = holder.findView(R.id.iv_three_one_two);
			ImageView ivThreeOneThree = holder.findView(R.id.iv_three_one_three);
			LinearLayout llThreeOneOne = holder.findView(R.id.ll_three_one_one);
			LinearLayout llThreeOneTwo = holder.findView(R.id.ll_three_one_two);
			LinearLayout llThreeOneThree = holder.findView(R.id.ll_three_one_three);
			TextView tvThreeOneOneTitle = holder.findView(R.id.tv_three_one_one_title);
			TextView tvThreeOneTwoTitle = holder.findView(R.id.tv_three_one_two_title);
			TextView tvThreeOneThreeTitle = holder.findView(R.id.tv_three_one_three_title);
			displayRandomImg(3, 0, ivThreeOneOne, data);
			displayRandomImg(3, 1, ivThreeOneTwo, data);
			displayRandomImg(3, 2, ivThreeOneThree, data);
			setOnClick(llThreeOneOne, data.get(0));
			setOnClick(llThreeOneTwo, data.get(1));
			setOnClick(llThreeOneThree, data.get(2));
			setDes(data, 0, tvThreeOneOneTitle);
			setDes(data, 1, tvThreeOneTwoTitle);
			setDes(data, 2, tvThreeOneThreeTitle);
		}
	}

	private void displayRandomImg(int imgNumber, int position, ImageView imageView, List<AndroidBean> object) {
		ImgLoadUtil.displayRandom(imgNumber, object.get(position).getImage_url(), imageView);
	}


	private void setOnClick(final LinearLayout linearLayout, final AndroidBean bean) {
		linearLayout.setOnClickListener(new PerfectClickListener() {
			@Override
			protected void onNoDoubleClick(View v) {
				WebViewActivity.loadUrl(v.getContext(), bean.getUrl(), "加载中...");
			}
		});

		linearLayout.setOnLongClickListener(new View.OnLongClickListener() {
			@Override
			public boolean onLongClick(View v) {
				AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());
				View view = View.inflate(v.getContext(), R.layout.title_douban_top, null);
				TextView titleTop = view.findViewById(R.id.title_top);
				titleTop.setTextSize(14);
				String title = TextUtils.isEmpty(bean.getType()) ? bean.getDesc() : bean.getType() + "：  " + bean.getDesc();
				titleTop.setText(title);
				builder.setCustomTitle(view);
				builder.setPositiveButton("查看详情", new DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						WebViewActivity.loadUrl(linearLayout.getContext(), bean.getUrl(), "加载中...");
					}
				});
				builder.show();
				return false;
			}
		});

	}
}
